package org.Vancl.Dao.ImpI;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.Vancl.Dao.userDao;
import org.Vancl.entity.User;
import org.Vancl.servlet.isLogiServlet;
import org.Vancl.utils.BaseDao;

public class userDaoImpl extends BaseDao implements userDao {

	
	public int adduser(User user) {
		String sql="insert into User(u_username,u_password,u_phone) values(?,?,?)";
		return super.executeUpdate(sql, user.getU_username(),user.getU_password(),user.getU_phone());
	}
	
	
	public String setCondition(User user, List<Object> params) {
		if (user == null) {
			return "";
		}
		String sql = "";
		String username = user.getU_username();
		if (username != null && !username.equals("")) {
			sql += " and u_username =?";
			//🍅吵�?
			params.add(username);
		}
		
		String pwd=user.getU_password();
		if(pwd != null && !pwd.equals("")) {
			sql += " and u_password = ?";
			params.add(pwd);
		}
		return sql;
	}
	
	
	public User get(User user) {
		String sql="SELECT * FROM USER where 1=1";
		List<Object> params = new ArrayList(); //用于封装？对应的参数值的
		sql+=setCondition(user, params);
		ResultSet rs=super.executeQuery(sql, params.toArray());
		User user2=null;
		try {
			while (rs.next()) {
				user2=new User();
				user2.setU_username(rs.getString("u_username"));
				user2.setU_password(rs.getString("u_password"));
				user2.setU_type(rs.getInt("u_type"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return user2;
	}



	public User getname(String name) {
		String sql="select * from User where u_username=?";
		ResultSet rs=super.executeQuery(sql, name);
		User user=null;
		try {
			while (rs.next()) {
				user=new User();
				user.setU_username(rs.getString("u_username"));
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return user;
	}
	/**
	 * 修改用户密码
	 */
	@Override
	public int update(User user) {
		String sql="UPDATE USER SET u_password=? WHERE u_username=?";
		return super.executeUpdate(sql, user.getU_password(),user.getU_username());
	}


	//修改个人资料
	@Override
	public int addMystuff(User user) {
		String sql="UPDATE USER SET `u_nickname`=?,`u_Name`=?,`u_sex`=?,`u_birthday`=?,`u_email`=?,`u_sheng`=?,`u_shi`=?,`u_xian`=?,`u_address`=?,`u_youzheng`=?,`u_phone`=?,`u_Images`=? WHERE `u_username`=?";
		return super.executeUpdate(sql, user.getU_nickname(),user.getU_name(),user.getU_sex(),user.getU_birthday(),user.getU_email(),user.getU_sheng(),user.getU_shi(),user.getU_xian(),user.getU_address(),user.getU_youzheng(),user.getU_phone(),user.getU_images(),user.getU_username());
	}


	//查询该用户的资料
	@Override
	public User selectname(String name) {
		String sql="SELECT * FROM USER WHERE u_username=?";
		ResultSet rs=super.executeQuery(sql, name);
		User u=null;
		try {
			if(rs.next()) {
				u=new User();
				u.setU_nickname(rs.getString("u_nickname"));
				u.setU_name(rs.getString("u_Name"));
				u.setU_sex(rs.getString("u_sex"));
				u.setU_birthday(rs.getString("u_birthday"));
				u.setU_email(rs.getString("u_email"));
				u.setU_sheng(rs.getString("u_sheng"));
				u.setU_shi(rs.getString("u_shi"));
				u.setU_xian(rs.getString("u_xian"));
				u.setU_address(rs.getString("u_address"));
				u.setU_youzheng(rs.getString("u_youzheng"));
				u.setU_phone(rs.getString("u_phone"));
				u.setU_images(rs.getString("u_Images"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return u;
	}
@Override
	public List<User> getAll() {
		String sql="select * from USER";
		ResultSet rs=super.executeQuery(sql);
		List<User> list=new ArrayList<>();
		try {
			while (rs.next()) {
				User user=new User();
				user.setU_id(rs.getInt("u_id"));
				user.setU_birthday(rs.getString("u_birthday"));
				user.setU_email(rs.getString("u_email"));
				user.setU_images(rs.getString("u_images"));
//				user.setU_money(rs.getString("u_money"));
				user.setU_name(rs.getString("u_name"));
				user.setU_nickname(rs.getString("u_nickname"));
				user.setU_password(rs.getString("u_password"));
				user.setU_phone(rs.getString("u_phone"));
				user.setU_sex(rs.getString("u_sex"));
				user.setU_username(rs.getString("u_username"));
				user.setU_type(rs.getInt("u_type"));
				list.add(user);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				super.CloseAll(rs, rs.getStatement().getConnection(), rs.getStatement());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}


	@Override
	public int state01(int id0) {
		String sql = "UPDATE USER SET u_type=1 WHERE u_id=?";
		return super.executeUpdate(sql, id0);
	}


	@Override
	public int state00(int id1) {
		String sql = "UPDATE USER SET u_type=0 WHERE u_id=?";
		return super.executeUpdate(sql, id1);
	}
}
